import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_button.dart';

/// 空数据占位图
///
/// dataList 数据源
/// imageName 图片名称
/// content 提示内容
/// buttonName 按钮文字
/// spacingTop
/// spacingBottom
/// imageSizeWidth
/// imageSizeHeight
/// onTapButton 按钮点击回调
/// containSliver 是否要包含Slivers组件，在Slivers系列组件里面使用
/// child 有数据的展示的主体
// ignore: non_constant_identifier_names
Widget XinDongEmptyContainer({
  required List? dataList,
  required Widget? child,
  String imageName = AssetsRes.IMAGE_EMPTY,
  String? emptyTips,
  String? buttonName,
  double spacingTop = 0.0,
  double spacingBottom = 0.0,
  double imageSizeWidth = kEmptyDataImageSize,
  double imageSizeHeight = kEmptyDataImageSize,
  Function()? onTapButton,
  bool containSliver = false,
  BoxFit boxFit = BoxFit.cover,
}) {
  const TextStyle style = TextStyle(fontSize: 15, fontWeight: FontWeight.normal, color: DZColors.placeholderColor);
  if (dataList == null) {
    return containSliver
        ? SliverToBoxAdapter(child: _htmy_loading(kEmptyDataImageSize, style))
        : _htmy_loading(kEmptyDataImageSize, style);
  } else if (dataList.isEmpty) {
    return containSliver
        ? SliverToBoxAdapter(
            child: _htmy_buildEmptyView(spacingTop, spacingBottom, imageSizeWidth, imageSizeHeight, imageName,
                emptyTips, style, buttonName, onTapButton, boxFit: boxFit))
        : _htmy_buildEmptyView(spacingTop, spacingBottom, imageSizeWidth, imageSizeHeight, imageName, emptyTips, style,
            buttonName, onTapButton,
            boxFit: boxFit);
  } else {
    return child ?? const Text('No child');
  }
}

/// 无数据框 - 给个大小，避免贴边
Center _htmy_buildEmptyView(
  double spacingTop,
  double spacingBottom,
  double imageSizeWidth,
  double imageSizeHeight,
  String imageName,
  String? content,
  TextStyle style,
  String? buttonName,
  Function()? onTapButton, {
  BoxFit boxFit = BoxFit.cover,
}) {
  return Center(
    child: Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(height: spacingTop),
        SizedBox(
          width: imageSizeWidth,
          height: imageSizeHeight,
          child: ImageUtil.asset(imageName, fit: boxFit),
        ),
        const SizedBox(height: 20),
        Text(content ?? "暂无数据", style: style),
        SizedBox(height: buttonName == null ? 0 : 20),
        buttonName == null
            ? const SizedBox()
            : XinDongCommonButton(buttonName, padding: const EdgeInsets.only(left: 30, right: 30), onTap: onTapButton),
        SizedBox(height: spacingBottom),
      ],
    ),
  );
}

/// 加载框 - 给个大小，避免贴边
Center _htmy_loading(double imageSize, TextStyle style) {
  return Center(
    child: Container(
      width: imageSize,
      height: imageSize,
      alignment: Alignment.center,
      child: Text("加载中", style: style),
    ),
  );
}
